#if 1
#define _BSD_SOURCE  1
#define _XOPEN_SOURCE
#endif
#include "Service.h"
#include "Constants.h"
#include "PublicUtils.h"
#include "CommandToken.h"
#include <termio.h>
#include <header.h>
#include "RequestHeader.h"
#include "RequestParameter.h"
#include "ResponseHeader.h"
#include "Global.h"
#include "Connection.h"
#include "ThreadPool.h"
#define SALT_LENGTH 16
#define SALT_PREFIX "$6$"  // 使用 SHA-512 算法

/**
 * @brief 注册
 * @author 牢大
*/
void userRegisterService(int net_fd){
   printf("请输入用户名\n");

   char uname[BUFF_SIZE];
   scanf("%s", uname);
   char *password = getpass("请输入密码\n");

   /* getpass没有读取回车 */
   char ch;
   while (ch != '\n' && ch != EOF) {
      ch = getchar();
   }

   /* 随机生成字符串 */
   char salt[SALT_LENGTH + 1];
   generateRandomString(salt, SALT_LENGTH);

   /* 构建标准盐值格式 */
   char saltFormatted[SALT_LENGTH + strlen(SALT_PREFIX) + 1];
   snprintf(saltFormatted, sizeof(saltFormatted), "%s%s", SALT_PREFIX, salt);

   
   /* 发送给服务器请求头 */
   RequestHeader* header = createRequestHeader(REGISTER, -1, -1, 2);
   send(net_fd, header, sizeof(RequestHeader), MSG_NOSIGNAL);
   
   /* 用链表优化 */
   RequestParameter *param = createLinkedList(2, uname, crypt(password, saltFormatted));
   sendLinkedList(net_fd, param);


   /* 接收服务端返回的Token */
   ResponseHeader *response = createResponseHeader(-1, "");
   recvResponseHeader(net_fd, response);

   if(response->flag == 0){
      printf("%s", response->data);
      return;
   }

   /* 存储Token到本地 */
   saveToken(response->data);

   /* 保存token为全局 */
   readToken();

   /* 设置标志位 */
   isLogin = 1;

   /* 设置当前目录为根目录 */
   strcat(currentDirectory, "/");
   strcat(currentDirectory, uname);

   freeRequestHeader(header);
   freeLinkedList(param);
   freeResponseHeader(response);
   
}

/**
 * @brief 登录
 * @author 牢大
*/
void userLoginService(int net_fd){
   printf("请输入用户名\n");
   char uname[BUFF_SIZE];
   // TODO 此处需要优化输入
   scanf("%s", uname);
   char *password = getpass("请输入密码\n");

   /* getpass没有读取回车 */
   char ch;
   while (ch != '\n' && ch != EOF) {
      ch = getchar();
   }

   /* 请求头的参数 */
   int argcnt = 2;
   int istk = readToken();
   if(istk == 0){
      argcnt = 1;
   }

   RequestHeader *head = createRequestHeader(LOGIN, -1, -1, argcnt);
   send(net_fd, head, sizeof(RequestHeader), MSG_NOSIGNAL);

   /* 发送用户名和token */
   if(argcnt == 2){
      RequestParameter *param = createLinkedList(argcnt, uname, token);
      sendLinkedList(net_fd, param);
      freeLinkedList(param);
   }else{
      RequestParameter *param = createLinkedList(argcnt, uname);
      sendLinkedList(net_fd, param);
      freeLinkedList(param);
   }


   ResponseHeader *response = createResponseHeader(-1, "");
   recv(net_fd, response, sizeof(ResponseHeader), MSG_WAITALL);

   if(response->flag == 1){
      /* 登录成功 */
      isLogin = 1;
      printf("%s", response->data);
      freeResponseHeader(response);

      strcat(currentDirectory, "/");
      strcat(currentDirectory, uname);

      /* 登录成功不需要释放token */
      return;
   }else if(response->flag == 0){
      /* 用户名错误 */
      printf("%s", response->data);
      freeResponseHeader(response);     
      return;
   }

   /* 接收的就是盐值，发送加密密码 */
   RequestParameter *cryptpwd = createLinkedList(1, crypt(password, response->data));
   sendLinkedList(net_fd, cryptpwd);
   freeLinkedList(cryptpwd);

   /* 接收判断是否成功 */
   ResponseHeader *response2 = createResponseHeader(-1, "");
   recv(net_fd, response2, sizeof(ResponseHeader), MSG_WAITALL);

   if(response2->flag == 0){
      /* 密码错误 */
      printf("%s", response2->data);
      freeResponseHeader(response2);
      freeResponseHeader(response);
     
      return;
   }

   saveToken(response2->data);
   /* 登录成功 */
   isLogin = 1;
   strcat(currentDirectory, "/");
   strcat(currentDirectory, uname);

   /* token需要重新复制 */
   strncpy(token, response2->data, BUFF_SIZE - 1);
   
   freeRequestHeader(head);
   freeResponseHeader(response2);
   freeResponseHeader(response);
}




